package com.uenpay.im.menu;


import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @Author: SL
 * @Date: 2016/11/4 16:27
 * @CopyRight: http://www.qiyunxin.com
 * @Parameter: TODO
 * @Function: 获取应用菜单分类
 */
public class GetMenusInvoke extends BaseHttpInvokeItem {
    public class GetHomeMenusInvokeResult extends HttpInvokeResult {
        public ArrayList<MenuEntity> arrayList = new ArrayList<>();
    }

    public GetHomeMenusInvokeResult getOutPut() {
        return (GetHomeMenusInvokeResult) GetResultObject();
    }

    public GetMenusInvoke() {
        String url = APIConfiguration.getMenuBaseUrl() + "v1/menus/svcs/qiyunxin?app_id=qiyunxin&is_tree=1";
        SetUrl(url);
    }

    @Override
    protected Object DeserializeResult(String responseBody) {
        GetHomeMenusInvokeResult result = new GetHomeMenusInvokeResult();
        try {
            JSONArray arr = new JSONArray(responseBody);
            for (int i = 0, length = arr.length(); i < length; i++) {
                JSONObject data = arr.optJSONObject(i);
                if (data != null) {
                    MenuEntity mMenuEntity = new MenuEntity();
                    mMenuEntity.name = data.optString("name");
                    mMenuEntity.flag = data.optString("flag");
                    mMenuEntity.icon_data = data.optString("icon_data");
                    mMenuEntity.mid = data.optString("mid");
                    mMenuEntity.icon_url = data.optString("icon_url");
                    mMenuEntity.name = data.optString("name");
                    mMenuEntity.permissions = data.optString("permissions");
                    mMenuEntity.pid = data.optString("pid");
                    mMenuEntity.json = data.optString("json");
                    JSONArray jsonArray = data.optJSONArray("children");
                    for (int j = 0, size = jsonArray.length(); j < size; j++) {

                        JSONObject dataJson = jsonArray.optJSONObject(j);
                        if (dataJson != null) {
                            MenuEntity children = new MenuEntity();
                            children.flag = dataJson.optString("flag");
                            children.icon_data = dataJson.optString("icon_data");
                            children.mid = dataJson.optString("mid");
                            children.icon_url = dataJson.optString("icon_url");
                            children.name = dataJson.optString("name");
                            children.permissions = dataJson.optString("permissions");
                            children.pid = dataJson.optString("pid");
                            children.json = dataJson.optString("json");
                            mMenuEntity.childrens.add(children);
                        }
                    }
                    result.arrayList.add(mMenuEntity);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
    }
}
